function Joypad() {
  this.x = 110;
  this.y = 500;

  this.angle = 13;
  this.tureAngle = 0;

  this.input = game.input;
  this.maxDistanceInPixels = 100;
  this.singleDirection = false;
}

Joypad.prototype.init = function() {
  var bRet = false;

  console.log('joyPad init ...');
  this.controlPanel1 = game.add.sprite(this.x, this.y, 's_Joypad1');
  this.controlPanel1.anchor.set(0.5);

  this.controlPanel2 = game.add.sprite(0, 0, 's_Joypad2');
  this.controlPanel2.anchor.set(0.5);
  
  
  this.controlSprite = game.add.sprite(0, 0, 's_Joypad3');
  this.controlSprite.anchor.set(0.5);

  this.controlPanel4 = game.add.sprite(0, 0, 's_Joypad4');
  this.controlPanel4.anchor.set(0.5);

  this.controlPanel1.addChild(this.controlPanel2)
  this.controlPanel1.addChild(this.controlSprite)
  this.controlPanel1.addChild(this.controlPanel4)
  this.updateRotation();
  bRet = true;

  this.inputEnable();
  return bRet;
}

Joypad.prototype.updateRotation = function(){
  this.controlSprite.rotation = this.angle + this.tureAngle;
};

Joypad.prototype.speed = {
  x: 0, y: 0
};

Joypad.prototype.cursors = {
  up: false, down: false, left: false, right: false
};

Joypad.prototype.touchStart = function(){
  this.controlPanel1.visible = true;
  this.controlPanel1.bringToTop();
  this.controlPanel1.cameraOffset.x = this.input.worldX;
  this.controlPanel1.cameraOffset.y = this.input.worldY;
  this.initialPoint = this.controlPanel1.position.clone();
  this.setDirection();
};

Joypad.prototype.setDirection = function() {
  var d = this.initialPoint.distance(this.input.activePointer.position);
  var maxDistanceInPixels = this.maxDistanceInPixels;

  var deltaX = this.input.activePointer.position.x - this.initialPoint.x;
  var deltaY = this.input.activePointer.position.y - this.initialPoint.y;

  if(this.singleDirection){
    if(Math.abs(deltaX) > Math.abs(deltaY)){
      deltaY = 0;
      this.input.activePointer.position.y = this.initialPoint.y;
    }else{
      deltaX = 0;
      this.input.activePointer.position.x = this.initialPoint.x;
    }
  }
  var angle = this.initialPoint.angle(this.input.activePointer.position);


  if (d > maxDistanceInPixels) {
    deltaX = (deltaX===0) ? 0 : Math.cos(angle) * maxDistanceInPixels;
    deltaY = (deltaY===0) ? 0 : Math.sin(angle) * maxDistanceInPixels;
    return ;
  } else {
    this.tureAngle = angle;
    this.updateRotation();
  }

  this.speed.x = parseInt((deltaX/maxDistanceInPixels) * 100 * -1, 10);
  this.speed.y = parseInt((deltaY/maxDistanceInPixels) * 100 * -1, 10);


  this.cursors.up = (deltaY < 0);
  this.cursors.down = (deltaY > 0);
  this.cursors.left = (deltaX < 0);
  this.cursors.right = (deltaX > 0);

  this.controlPanel1.cameraOffset.x = this.initialPoint.x + (deltaX) / 3;
  this.controlPanel1.cameraOffset.y = this.initialPoint.y + (deltaY) / 3;
};

Joypad.prototype.removeTouch = function(){
  this.cursors.up = false;
  this.cursors.down = false;
  this.cursors.left = false;
  this.cursors.right = false;

  this.speed.x = 0;
  this.speed.y = 0;

};

Joypad.prototype.inputEnable = function() {
  this.input.onDown.add(this.touchStart, this);
  this.input.onUp.add(this.removeTouch, this);
};

Joypad.prototype.inputDisable = function() {
  this.input.onDown.remove(this.touchStart, this);
  this.input.onUp.remove(this.removeTouch, this);
};

Joypad.prototype.update = function() {

}
